// pages/userInfoList/userInfoList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    UserName: '',
    UserHead: '/images/default-head.png',
    Sex: 0,
    UserDesc: '',
    UserHeadUrl: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getData();
  },
  onShow: function() {
    let self = this;
  },
  //图片上传
  upImgFile: function(imgUrl) {
    let self = this;
    wx.uploadFile({
      url: app.globalData.apiRootUrl + '/Common/FileUpload',
      filePath: imgUrl[0],
      name: 'file',
      formData: {},
      success: function(res) {
        let rdata = JSON.parse(res.data);
        if (rdata.status == 0) {
          self.setData({
            UserHeadUrl: imgUrl[0],
            UserHead: rdata.data
          })
        } else {
          wx.showToast({
            title: rdata.message,
            icon: 'none'
          })
        }
      },
      fail: function(e) {
        wx.showToast({
          title: '上传图片失败',
          icon: 'none'
        })
      }
    })
  },
  // 监听文本域输入
  textAreaInput: function(e) {
    let self = this;
    self.setData({
      UserDesc: e.detail.value
    })

  },
  /**
   * 表单提交
   */
  formSubmit: function(e) {
    let self = this;
    let data = e.detail.value;
    data.Sex = self.data.Sex;
    data.UserHead = self.data.UserHead;
    data.UserID = app.globalData.userInfo.UserID;

    let url = app.globalData.apiRootUrl + "/User/EditUser";
    app.httpRequest({
      url: url,
      method: "POST",
      data: data,
      success: function(res) {
        //console.log(res);
        let rdata = res.data;
        if (rdata.status == 0) {
          app.globalData.userInfo.UserName = data.UserName;
          app.globalData.userInfo.UserHead = data.UserHead || '/images/default-head.png';
          app.globalData.userInfo.Sex = data.Sex;
          app.setUserInfo(app.globalData.userInfo);
          wx.navigateBack();
        } else {
          wx.showToast({
            title: rdata.data.message,
            icon: "none"
          })
        }
      }
    })
  },
  //更换头像
  changeHeadImg: function() {
    let self = this;
    wx.showActionSheet({
      itemList: ['拍照', '从手机相册选择'],
      success(res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) { //'拍照'
          self.chooseImage('camera');
        } else if (res.tapIndex == 1) { //从手机相册选择
          self.chooseImage('album');
        }
      },
      fail(res) {
        console.log(res.errMsg);
      }
    })

  },
  //选择图片
  chooseImage: function(flag) {
    let self = this;
    let flagSouse = flag;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: [flagSouse],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        self.upImgFile(tempFilePaths);
      }
    })
  },
  //更改性别
  changeUserGender: function() {
    let self = this;
    wx.showActionSheet({
      itemList: ['男', '女'],
      success(res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) {
          self.setData({
            SexName: '男',
            Sex: 1
          })
          self.updateUserGender(1);
        } else if (res.tapIndex == 1) {
          self.setData({
            SexName: '女',
            Sex: 2
          });
        }
      },
      fail(res) {
        console.log(res.errMsg);
      }
    })
  },
  // //退出登录
  // logoutTap: function() {
  //   let self = this;
  //   wx.reLaunch({
  //     url: '/pages/index/index',
  //   })
  //   // self.globalData = {};
  //   wx.clearStorageSync();
  //   //获取wxSession 登录
  //   //app.wxLogin();
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 获取文章字符串
   */
  getData: function() {
    let self = this;
    let url = app.globalData.apiRootUrl + "/User/GetUserInfo";
    app.httpRequest({
      url: url,
      data: {
        userId: app.globalData.userInfo.UserID
      },
      success: function(res) {
        //console.log(res);
        let rdata = res.data;
        if (rdata.status == 0 && rdata.data) {
          self.setData({
            UserName: rdata.data.UserName,
            UserHead: rdata.data.UserHead,
            UserHeadUrl: rdata.data.UserHead,
            Sex: rdata.data.Sex,
            UserDesc: rdata.data.UserDesc,
            SexName: rdata.data.Sex == 1 ? '男' : '女'
          })

        }
      }
    })
  }
})